package com.trisan;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Configuration
@RequestMapping("/ecntr")
public class MyController {
	
	@Autowired
	EmpDAO empDAO;
	
	@Autowired
	DeptDAO deptDAO;
	
	@RequestMapping("/wel")
	public ModelAndView getWel(){
		ModelAndView mav=new ModelAndView("welcome");
		return mav;
	}
	
	@RequestMapping("/welthead")
	public ModelAndView getWelco(){
		ModelAndView mav=new ModelAndView("TestTypehead");
		return mav;
	}
	
	
	
	@RequestMapping("/create")
	public String createEmp(@ModelAttribute("emp")Emp emp,Model model){
		
		if(empDAO.save(emp)){
			model.addAttribute("status", "suceefully saved with id"+emp.id);
		}
		else{
			model.addAttribute("status", "failed to save Emp,plz check details");
		}
		return "";
	}
	
	
	@RequestMapping("/upd")
	public String updateEmp(@ModelAttribute("emp")Emp emp,Model model){
		if(empDAO.update(emp)){
			model.addAttribute("status", "updated sucessfully");
		}
		else{
			model.addAttribute("status", "unable to update");
		}
		return "";
	}
	
	@RequestMapping("/dlt")
	public String deleteEmp(@ModelAttribute("emp")Emp emp,Model model){
		if(empDAO.delete(emp)){
			model.addAttribute("status", "deleted sucessfully");
		}
		else{
			model.addAttribute("status", "unable to update");
		}
		return "";
	}
	
	@RequestMapping("/ge")
	public String getEmp(@ModelAttribute("emp")Emp emp,Model model){
		model.addAttribute("emp1",empDAO.getEmp(emp.getId()));
		return "SendNames";
	}
	
	
	@RequestMapping("/ges")
	public String getEmployees(Model model){
			model.addAttribute("emplist", empDAO.getEmps());
		return "";
	}
	
	@RequestMapping(value="/gnames",method = RequestMethod.GET)
	public String getNames(@RequestParam("q") String name,Model model){
		model.addAttribute("namesList", empDAO.getEmpNames(name));
			return "SendNames";
	}
	
	@RequestMapping(value="/gnslist",method = RequestMethod.GET)
	public @ResponseBody List<Emp> getNamesList(Model model){
	
			return empDAO.getEmps();
	}
	
	@RequestMapping(value="/gdpts",method = RequestMethod.GET)
	public @ResponseBody List<Dept> getDepts(Model model){
		return deptDAO.getDepts();
	}
	
	@RequestMapping("/frt")
	public String getFrontPage(){
		return "FrontPage";
	}
}
